package com.tencreat.service.account.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.redis.RedisCache;
import com.tencreat.common.utils.DictUtils;
import com.tencreat.common.utils.spring.SpringUtils;
import org.springframework.stereotype.Service;
import com.tencreat.mapper.account.MallSystemMapper;
import com.tencreat.domain.account.MallSystem;
import com.tencreat.service.account.IMallSystemService;

/**
 * 系统配置Service业务层处理
 *
 * @author tencreat
 * @date 2021-09-13
 */
@Service
public class MallSystemServiceImpl extends ServiceImpl<MallSystemMapper, MallSystem> implements IMallSystemService {


    /**
     * 查询系统配置
     *
     * @param id 系统配置ID
     * @return 系统配置
     */
    @Override
    public MallSystem selectMallSystemById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询系统配置列表
     *
     * @param mallSystem 系统配置
     * @return 系统配置
     */
    @Override
    public List<MallSystem> selectMallSystemList(MallSystem mallSystem) {
        return baseMapper.selectList(Wrappers.query(mallSystem));
    }

    /**
     * 新增系统配置
     *
     * @param mallSystem 系统配置
     * @return 结果
     */
    @Override
    public int insertMallSystem(MallSystem mallSystem) {
        mallSystem.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(mallSystem);
    }

    /**
     * 修改系统配置
     *
     * @param mallSystem 系统配置
     * @return 结果
     */
    @Override
    public int updateMallSystem(MallSystem mallSystem) {
        mallSystem.setUpdateTime(LocalDateTime.now());
        mallSystem.setDelFlag("0");
        return baseMapper.updateById(mallSystem);
    }

    /**
     * 批量删除系统配置
     *
     * @param ids 需要删除的系统配置ID
     * @return 结果
     */
    @Override
    public int deleteMallSystemByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除系统配置信息
     *
     * @param id 系统配置ID
     * @return 结果
     */
    @Override
    public int deleteMallSystemById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public MallSystem selectMallSystemByName(String name) {
        return lambdaQuery().eq(MallSystem::getKeyName,name).one();
    }

    @Override
    public int editKeyValueByName(MallSystem mallSystem) {
        MallSystem system = lambdaQuery().eq(MallSystem::getKeyName, mallSystem.getKeyName()).one();
        system.setKeyValue(mallSystem.getKeyValue());
        // 清空缓存
        SpringUtils.getBean(RedisCache.class).deleteObject(mallSystem.getKeyValue());
        return baseMapper.updateById(system);
    }
}
